package com.zzhrtech.wgzx_cityzen.ui.contact;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.GsonRequest;
import com.zzhrtech.WgzxApplication;
import com.zzhrtech.wgzx_cityzen.BaseActivity;
import com.zzhrtech.wgzx_cityzen.Constans;
import com.zzhrtech.wgzx_cityzen.MainActivity;
import com.zzhrtech.wgzx_cityzen.R;
import com.zzhrtech.wgzx_cityzen.adapters.contact.JifenListAdapter;
import com.zzhrtech.wgzx_cityzen.beans.contact.JifenDhBean;
import com.zzhrtech.wgzx_cityzen.beans.contact.JifenGoodsDetailBean;
import com.zzhrtech.wgzx_cityzen.beans.me.LoginBean;
import com.zzhrtech.wgzx_cityzen.beans.task.BaseBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JifenDhActivity extends BaseActivity {

    private RecyclerView recy_jfdh;
    private JifenListAdapter listAdapter;

    private MaterialDialog progress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jifen_dh);

        toolbar.setNavigationIcon(R.mipmap.ic_close);


        recy_jfdh = (RecyclerView) findViewById(R.id.recy_jfdh);
        recy_jfdh.setLayoutManager(new LinearLayoutManager(this));
        recy_jfdh.setItemAnimator(new DefaultItemAnimator());

        listAdapter = new JifenListAdapter(this);
        recy_jfdh.setAdapter(listAdapter);

        httpGetGoods();
    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 0) {

                progress.dismiss();

                List<JifenDhBean.Data> datas = (List<JifenDhBean.Data>) msg.obj;

                listAdapter.cleanList();
                for (JifenDhBean.Data data : datas){
                    listAdapter.addItem(data);
                }

            }else if (msg.what == 1){
                progress.dismiss();
                showToast("已撤销");

                httpGetGoods();
            }

            return false;
        }
    });


    private void httpGetGoods(){

        progress = new MaterialDialog.Builder(this)
                .title("请等待")
                .content("加载中")
                .progress(true,0)
                .show();


        Map<String,String> map = new HashMap<String, String>();
        map.put("citizen_id",getCache().getAsString(keyUid));


        GsonRequest<JifenDhBean> gsonRequest = new GsonRequest<JifenDhBean>(Request.Method.POST, Constans.HTTP_GETSGOODS, JifenDhBean.class, map, new com.android.volley.Response.Listener<JifenDhBean>() {
            @Override
            public void onResponse(JifenDhBean response) {

                if (response.isSuccess()) {
                    Message message = new Message();
                    message.what = 0;
                    message.obj = response.getData();
                    mHandler.sendMessage(message);
                }else {
                    progress.dismiss();
                    showToast(response.getMsg());
                }

            }
        }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progress.dismiss();

                Log.d("result=====",error + "");
//                dialog.dismiss();
            }
        });

        WgzxApplication.addRequest(gsonRequest, Constans.reqTag);
    }

    public void httpGoodRefund(String id){

        progress = new MaterialDialog.Builder(this)
                .title("请等待")
                .content("加载中")
                .progress(true,0)
                .show();


        Map<String,String> map = new HashMap<String, String>();
        map.put("id",id);


        GsonRequest<BaseBean> gsonRequest = new GsonRequest<BaseBean>(Request.Method.POST, Constans.HTTP_GOODREFUND, BaseBean.class, map, new com.android.volley.Response.Listener<BaseBean>() {
            @Override
            public void onResponse(BaseBean response) {

                if (response.isSuccess()) {
                    Message message = new Message();
                    message.what = 1;
                    mHandler.sendMessage(message);
                }else {
                    progress.dismiss();
                    showToast(response.getMsg());
                }

            }
        }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progress.dismiss();

                Log.d("result=====",error + "");
//                dialog.dismiss();
            }
        });

        WgzxApplication.addRequest(gsonRequest, Constans.reqTag);
    }



}
